<template>
	<el-dialog :title="form.id ? '编辑' : '新增'" :visible.sync="visible" :close-on-click-modal="false">
		<el-form ref="form" :model="form" :rules="rules">
			<el-form-item label="标题" :label-width="formLabelWidth" prop="title">
				<el-input v-model="form.title" autocomplete="off" placeholder="请输入标题"></el-input>
			</el-form-item>
			<el-form-item label="内容" :label-width="formLabelWidth" prop="content">
				<fortress-wang-editor v-model="form.content"></fortress-wang-editor>
				<!-- <el-input v-model="form.content" autocomplete="off" placeholder="请输入内容" type="textarea"></el-input> -->
			</el-form-item>
			<el-form-item v-if="!form.id" label="选择会员" :label-width="formLabelWidth">
				<el-radio-group v-model="form.type">
					<el-radio :label="1">全部会员</el-radio>
					<el-radio :label="2">指定会员</el-radio>
				</el-radio-group>
				<br />
				<el-select style="width: 80%" v-if="form.type == 2" clearable filterable v-model="chooseMember" multiple placeholder="请选择">
					<el-option v-for="item in memberList" :key="item.id" :label="item.username" :value="item.id"></el-option>
				</el-select>
			</el-form-item>
		</el-form>
		<div slot="footer" class="dialog-footer">
			<el-button @click="visible = false" size="medium" type="danger">取 消</el-button>
			<el-button type="primary" size="medium" @click="submitForm('form')">确 定</el-button>
		</div>
	</el-dialog>
</template>
<script>
import { membernoticeInfo, membernoticeSave, membernoticeEdit } from "@/api/member/membernotice.js"
import { memberList } from "@/api/member/member.js"

export default {
	data() {
		return {
			visible: false,
			form: {
				title: "",
				content: "",
				type: 1,
				memberIds: "",
			},
			formLabelWidth: "120px",
			rules: {
				title: [{ required: true, message: "请输入标题", trigger: "blur" }],
				content: [{ required: true, message: "请输入内容", trigger: "blur" }],
			},
			memberList: [],
			chooseMember: [],
		}
	},
	created() {
		this.getMemberList()
	},
	methods: {
		getMemberList() {
			memberList().then(res => {
				this.memberList = res.data
			})
		},
		init(id) {
			this.resetForm()
			//修改
			if (id) {
				membernoticeInfo(id).then(resp => {
					if (resp.code == 0) {
						this.$nextTick(() => {
							this.form = {
								...this.form,
								...resp.data,
							}
						})
					} else {
						this.$msg({
							message: resp.msg,
							type: "error",
						})
					}
				})
			}
			this.visible = true
		},
		//提交表单数据
		submitForm() {
			console.log(this.chooseMember.toString())
			this.$refs["form"].validate(valid => {
				if (valid) {
					//修改
					if (this.form.id) {
						membernoticeEdit(this.form).then(resp => {
							if (resp.code == 0) {
								this.$message({
									type: "success",
									message: "操作成功!",
								})
								this.$emit("refreshData")
								this.visible = false
							} else {
								this.$msg({
									message: resp.msg,
									type: "error",
								})
							}
						})
						//新增
					} else {
						if (this.form.type == 2 && this.chooseMember.length == 0) {
							this.$message.error("请选择接收消息的会员")
							return
						}
						this.form.memberIds = this.chooseMember.toString()
						membernoticeSave(this.form).then(resp => {
							if (resp.code == 0) {
								this.$message({
									type: "success",
									message: "操作成功!",
								})
								this.$emit("refreshData")
								this.visible = false
							} else {
								this.$msg({
									message: resp.msg,
									type: "error",
								})
							}
						})
					}
				} else {
					console.log("error submit!!")
					return false
				}
			})
		},
		resetForm() {
			this.form = {
				title: "",
				content: "",
				type: 1,
				memberIds: "",
			}
			this.$nextTick(() => {
				if (this.$refs["form"]) {
					this.$refs["form"].resetFields()
				}
			})
		},
	},
}
</script>
<style scoped>
.avatar-uploader .el-upload {
	border: 1px dashed #d9d9d9;
	border-radius: 6px;
	cursor: pointer;
	position: relative;
	overflow: hidden;
}
.avatar-uploader .el-upload:hover {
	border-color: #409eff;
}
.avatar-uploader-icon {
	font-size: 28px;
	color: #8c939d;
	width: 178px;
	height: 178px;
	line-height: 178px;
	text-align: center;
}
.avatar {
	width: 178px;
	height: 178px;
	display: block;
}
.el-scrollbar .el-scrollbar__view .el-select-dropdown__item {
	height: auto;
	max-height: 274px;
	padding: 7px;
	overflow: hidden;
	overflow-y: auto;
	font-weight: normal;
}
</style>
